var moyu2 = cc.Class({
    extends: cc.Component,
    properties: () => ({
        moyuPos : 0,                              // 最后的位置值初始为0
        moyuScore : 0,                            // 墨鱼分数值 初始为null
        moyuStatusA : 0,                          // 正面状态 初始为0生，1半生，2金，3焦
        moyuStatusB : 0,                          // 背面状态 初始为0生，1半生，2金，3焦
        getOver: cc.Boolean,                           // 是否被收起
    }),
    onLoad: function() {
        //this.passATime = 0;                          // 当前A面经过时间1秒
        //this.passBTime = 0;                          // 当前B面经过时间1秒
        this.aTime = 0;
        this.bTime = 0;
        this.curFace = true;    //true正面朝上，烤B面
        this.getOver = false;
        this.bansheng   = 5.0;    //生的状态，低于5秒
        this.shu        = 10.0;   //半生熟，介于5-10秒之间
        this.jiao       = 15.0;   //熟，介于10-15秒之间。大于15秒都是焦

    },
    update: function(dt) {
        if(!Globals.gameRun) return;
        
        if(this.getOver) return;
        
        var Yan = this.node.getChildByName('yan');
        var aniYan = this.node.getChildByName('yan').getComponent(cc.Animation);
        var Smoke = this.node.getChildByName('smoke');
        var aniSmoke = this.node.getChildByName('smoke').getComponent(cc.Animation);

        //烧烤面的计时
        var curTime = 0; //当前被烧烤的面的计时器时间
        var curMoyuStatus = 0;
        if(this.curFace)
        {
            this.bTime += dt * Globals.powerBS;
            curTime = this.bTime;
            curMoyuStatus = this.moyuStatusB;
        }
        else 
        {
            this.aTime += dt * Globals.powerBS;
            curTime = this.aTime;
            curMoyuStatus = this.moyuStatusA;
        }

        
        if(curMoyuStatus == 0 && curTime >= this.bansheng)
        {
            //切换状态
            this.curFace ? this.moyuStatusB = 1 : this.moyuStatusA = 1;
            
            //冒烟+动画
            Yan.active = true;
            aniYan.playAdditive('yan');
            aniYan.on("lastframe",this.onend,this);
            this.moyuRun();
        }
        else if(curMoyuStatus == 1 && curTime >= this.shu)
        {
            this.curFace ? this.moyuStatusB = 2 : this.moyuStatusA = 2;
            //冒烟+动画
            Yan.active = true;
            aniYan.playAdditive('yan');
            aniYan.on("lastframe",this.onend,this);
            this.moyuRun();

            //显示收货按钮
            this.node.getChildByName('btnRed').active = true;
        }
        else if(curMoyuStatus == 2 && curTime >= this.jiao)
        {
            this.curFace ? this.moyuStatusB = 3 : this.moyuStatusA = 3;
            //冒‘黑’烟+动画
            Smoke.active = true;
            aniSmoke.playAdditive('smoke');
            aniSmoke.on("lastframe",this.onend,this);
            this.moyuRun();
        }


        /*

        if(this.curFace) {
            this.bTime += dt;
            this.passBTime = Math.floor(this.bTime += dt);
            curTime = this.passBTime;
            if(this.moyuStatusB == 0 && curTime == Globals.sheng) {
                this.moyuStatusB = 1;
                Yan.active = true;
                aniYan.playAdditive('yan');
                aniYan.on("lastframe",this.onend,this);
                this.moyuRun();
                curTime = Globals.sheng;
            }else if(this.moyuStatusB == 1 && curTime == Globals.bansheng) {
                this.node.getChildByName('btnRed').active = true;
                this.moyuStatusB = 2;
                Yan.active = true;
                aniYan.playAdditive('yan');
                aniYan.on("lastframe",this.onend,this);
                this.moyuRun();
                curTime = Globals.bansheng;
            }else if(this.moyuStatusB == 2 && curTime == Globals.shou) {
                this.moyuStatusB = 3;
                Smoke.active = true;
                aniSmoke.playAdditive('smoke');
                aniSmoke.on("lastframe",this.onend,this);
                this.moyuRun();
                curTime = Globals.shou;
            }
        }else{
            this.aTime += dt;
            this.passATime = Math.floor(this.aTime += dt);
            curTime = this.passATime;
            if(this.moyuStatusA == 0 && curTime == Globals.sheng) {
                this.moyuStatusA = 1;
                Yan.active = true;
                aniYan.playAdditive('yan');
                aniYan.on("lastframe",this.onend,this);
                this.moyuRun();
                curTime = Globals.sheng;
            }else if(this.moyuStatusA == 1 && curTime == Globals.bansheng) {
                this.node.getChildByName('btnRed').active = true;
                this.moyuStatusA = 2;
                Yan.active = true;
                aniYan.playAdditive('yan');
                aniYan.on("lastframe",this.onend,this);
                this.moyuRun();
                curTime = Globals.bansheng;
            }else if(this.moyuStatusA == 2 && curTime == Globals.shou) {
                this.moyuStatusA = 3;
                Smoke.active = true;
                aniSmoke.playAdditive('smoke');
                aniSmoke.on("lastframe",this.onend,this);
                this.moyuRun();
                curTime = Globals.shou;
            }
        }
        */
    },
    // 烟隐藏
    onend: function() {
        this.node.getChildByName('yan').active = false;
        this.node.getChildByName('smoke').active = false;
        animation.off("lastframe",this.onend,this);
    },
    // 正面效果
    moyuRun: function() {
        for(var i = 0; i < 8; i++) {
            if(this.node.children[i].active) {
                var animation = this.node.children[i].getComponent(cc.Animation);
                animation.playAdditive(animation._clips[0].name);
            }
        }
    },
    getMoyuPos:function()
    {
        return this.moyuPos;
    },
    setMoyuPos:function(pos_i)
    {
        this.moyuPos = pos_i;
    },
    changFace: function() {
        if(this.curFace) {
            this.getColorAAA(this.moyuStatusB);
        }else if(!this.curFace){
            this.getColorBBB(this.moyuStatusA);
        }
        this.curFace =! this.curFace;
    },
    getColorAAA: function(staus) {
        if(staus == 0) {
            this.node.getChildByName('yu12').active = true;
            this.colorAAA();
        }else if(staus == 1) {
            this.node.getChildByName('yu22').active = true;
            this.colorAAA();
        }else if(staus == 2) {
            this.node.getChildByName('yu32').active = true;
            this.colorAAA();
        }else {
            this.node.getChildByName('yu42').active = true;
            this.colorAAA();
        }
    },
    colorAAA: function() {
        for(var i = 0 ; i < 4; i++) {
            if(this.node.children[i].active) {
                this.node.children[i].active = false;
            }
        }
    },
    getColorBBB: function(staus) {
        if(staus == 0) {
            this.node.getChildByName('yu11').active = true;
            this.colorBBB();
        }else if(staus == 1) {
            this.node.getChildByName('yu21').active = true;
            this.colorBBB();
        }else if(staus == 2) {
            this.node.getChildByName('yu31').active = true;
            this.colorBBB();
        }else {
            this.node.getChildByName('yu41').active = true;
            this.colorBBB();
        }
    },
    colorBBB: function() {
        for(var i = 4 ; i < 8; i++) {
            if(this.node.children[i].active) {
                this.node.children[i].active = false;
            }
        }
    },
    setScore:function()
    {
        var ABStatus = null;
        ABStatus = this.moyuStatusA.toString() + this.moyuStatusB.toString();
        
        /*
        if(ABStatus == '03' || ABStatus == '30' || ABStatus == '33'){
            this.moyuScore = 0;
        }
        */

        if(this.moyuStatusA == 3 || this.moyuStatusB == 3)
        {
            this.moyuScore = 0 * Globals.resultCoin;
        }
        else
        {
            if(ABStatus == '00'){
                this.moyuScore = 0 * Globals.resultCoin;
            }
            else if(ABStatus == '01' || ABStatus == '10'){
                this.moyuScore = 0 * Globals.resultCoin;
            }
            else if(ABStatus == '02' || ABStatus == '20'){
                this.moyuScore = 1 * Globals.resultCoin;
            }
            else if(ABStatus == '12' || ABStatus == '21'){
                this.moyuScore = 2 * Globals.resultCoin;
            }
            else if(ABStatus == '22'){
                this.moyuScore = 5 * Globals.resultCoin;
            }else this.moyuScore = 0 * Globals.resultCoin;
        }

        
        this.getOver = true;
        // cc.log(this.getOver,this.moyuScore,ABStatus,'点击了');
    },
});
module.exports = moyu2;
